package com.jcfy.project.module.controller;

import java.util.List;

import com.jcfy.project.module.domain.TUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jcfy.framework.aspectj.lang.annotation.Log;
import com.jcfy.framework.aspectj.lang.enums.BusinessType;
import com.jcfy.project.module.domain.TGoods;
import com.jcfy.project.module.service.ITGoodsService;
import com.jcfy.framework.web.controller.BaseController;
import com.jcfy.framework.web.domain.AjaxResult;
import com.jcfy.common.utils.poi.ExcelUtil;
import com.jcfy.framework.web.page.TableDataInfo;

/**
 * 商品Controller
 *
 * @author jiangfeng
 * @date 2020-08-05
 */
@RestController
@RequestMapping("/module/goods" )
public class TGoodsController extends BaseController {
    @Autowired
    private ITGoodsService tGoodsService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('module:goods:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(TGoods tGoods) {
        startPage();
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('module:goods:export')" )
    @Log(title = "商品" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(TGoods tGoods) {
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        ExcelUtil<TGoods> util = new ExcelUtil<TGoods>(TGoods. class);
        return util.exportExcel(list, "goods" );
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('module:goods:query')" )
    @GetMapping(value = "/{goodsId}" )
    public AjaxResult getInfo(@PathVariable("goodsId" ) Long goodsId) {
        return AjaxResult.success(tGoodsService.selectTGoodsById(goodsId));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('module:goods:add')" )
    @Log(title = "商品" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TGoods tGoods) {
        return toAjax(tGoodsService.insertTGoods(tGoods));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('module:goods:edit')" )
    @Log(title = "商品" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TGoods tGoods) {
        return toAjax(tGoodsService.updateTGoods(tGoods));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('module:goods:remove')" )
    @Log(title = "商品" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}" )
    public AjaxResult remove(@PathVariable Long[] goodsIds) {
        return toAjax(tGoodsService.deleteTGoodsByIds(goodsIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('module:goods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping("/recommendGoods")
    public AjaxResult recommendGoods(@RequestBody TGoods tGoods) {
        return toAjax(tGoodsService.updateTGoods(tGoods));
    }
}
